import {Actions} from "react-native-router-flux";

import moment from 'moment'
import {InSupervision} from "../../services/indexPage";
import {Toast} from 'antd-mobile'

function showToast(msg) {
  Toast.fail(msg, 2)
}

export default {
  namespace: 'projectsList',

  state: {
    list: [],
    projectId: '',
    page:1,
    pageSize:9,
    last:0,
    loading:true,
    tabNum:0,
    role:'',
    dialogText:'',
    prjname:''
  },


  effects: {
    *getId({payload},{select,put,call}){
      yield put({type:'projectDetail/getPrjId',payload:payload})
      Actions.push('ProjectDetail')
    },
      *getTableList({payload}, {put, select, call}){
          yield put({type:'getLoading'})
          let current = '';
          let TabNum = '';
          const {role} = yield select(state =>state.login);
          const {page, pageSize,tabNum,prjname} = yield select(state => state.projectsList)
          if(payload === '' || payload === undefined){
              current = page;
              TabNum = tabNum;
          }else {
              if (payload.page ==='' || payload.page === undefined){
                  current = page;
              }else {
                  current = payload.page;
              }
              if (payload.tabNum ==='' || payload.tabNum === undefined){
                  TabNum = tabNum;
              }else {
                  TabNum = payload.tabNum;
              }
          }
          const {json, error} = yield call(InSupervision, {pageNo: current, pageSize: pageSize,prjname:prjname})
          if (error) {
              console.log('Query InSupervision failed: ' + error)
              yield put({type: 'cleanLoading'})
          } else {
            yield put({type: 'querySuccess', payload: {res:json.data,role:role}})
          }

      },
      *reset(){

      },
      *dialogShow({payload}, {put, select, call}){
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShowText',payload:'功能暂未开发'});
      },
      *search({payload},{call,put,select}){
          yield put({type:'getTableList',payload:{page:1}});
      }
  },

  reducers: {
    querySuccess(state, {payload}){
      let num = payload.res.pageNo;
      let last = payload.res.last;
      let list = payload.res.list.map(val => {
        let tmp = {
          projectId: val.id,
          tableData: [
            val.prjname,
            val.planStartDate !== '' ? moment(val.planStartDate).format('YYYY-MM-DD') : '-',
            val.planCompleteDate !== '' ? moment(val.planCompleteDate).format('YYYY-MM-DD') : '-',
          ]
        }
        return tmp
      })

      return {...state, list: list,page:num,last:last,loading:false,role:payload.role}
    },
    setProjectId(state, {payload}){
      return {...state, projectId: payload}
    },
    getLoading(state,{payload}){
      return {...state,loading:true}
    },
    cleanLoading(state,{payload}){
      return{...state,loading:false}
    },
    getClean(state,{payload}){
      return {...state,loading:true,page:1,prjname:''}
    },
    dialogShowText(state,{payload}){
      return {...state,dialogText:payload}
    },
    changeText(state,{payload}){
        return {...state,prjname:payload}
    }
  },

}